boxee.autoChoosePlayer=true;
boxee.renderBrowser=false;
boxee.enableLog(true);
boxee.setCanPause(true);
boxee.setCanSkip(true);
boxee.log('Testing remote');

var x_start = 77;
var x_end = 512;
var y = 590;
var current_x = x_start;
var skip = 10;
var skip_big = 50;

//function R(p) { return (p & 0x00ff0000) >> 16; }
// function G(p) { return (p & 0x0000ff00) >> 8; }
// function B(p) { return (p & 0x000000ff) ; }

function getProgressPrecise(widget, start, end)
{
	var previous = 0;
	for(var x=end; x>start; x--) {
		color = widget.getPixelData(x,y);
		r = color.r;
		if(previous > 0 && previous != r) {
			boxee.log("pixel2:"+r);
			return x;
		}
		previous = r;
	}
}

function getProgressCoordinate(widget)
{
	boxee.log("getProgressCoordinate");
	var pixel = 0;
	var previous = 0;
	
	for(var x=x_start; x<x_end; x=x+20) {
		color = widget.getPixelData(x,y);
		r = color.r;
		g = color.g;
		b = color.b;
		if(r == g && g == b) {
			if(previous > 0 && previous != r && (r == 51 || r == 206 || r == 167 || r == 81 || r == 118)) {
				// boxee.log("prev:"+previous);
				// boxee.log("r:"+r);
				return getProgressPrecise(widget, x-20, x)
			}
			previous = r;
		}
		//boxee.log("Pixel at "+x+","+y+" is "+R);
	}
	boxee.log("current_x:"+current_x);
	return current_x;
}

boxee.onPause = function()
{
   	boxee.log('user pressed pause');
	widget = boxee.getActiveWidget();
	widget.click(1,1);
}
 
boxee.onPlay = function()
{
   	boxee.log('user pressed play');
	widget = boxee.getActiveWidget();
	widget.click(1,1);
}

boxee.onBack = function()
{
	boxee.log('user pressed back')
	widget = boxee.getActiveWidget();
	//widget.mouseMove(10, 10);
	current_x = getProgressCoordinate(widget);
	current_x = current_x - skip;
	if(current_x < x_start) current_x = x_start;
	if(current_x > x_end) current_x = x_end;
	widget.click(current_x, y);
	boxee.notifyPlaybackResumed();
}

boxee.onSkip = function()
{
	boxee.log("user pressed skip")
	widget = boxee.getActiveWidget();
	//widget.mouseMove(10, 10);
	current_x = getProgressCoordinate(widget);
	current_x = current_x + skip;
	if(current_x < x_start) current_x = x_start;
	if(current_x > x_end) current_x = x_end;
	widget.click(current_x, y);
	boxee.notifyPlaybackResumed();
	boxee.log("notifyPlaybackResumed");
}

boxee.onBigBack = function()
{
	boxee.log('user pressed big back')
	widget = boxee.getActiveWidget();
	//widget.mouseMove(10, 10);
	current_x = getProgressCoordinate(widget);
	current_x = current_x - skip_big;
	if(current_x < x_start) current_x = x_start;
	if(current_x > x_end) current_x = x_end;
	widget.click(current_x, y);
	boxee.notifyPlaybackResumed();
}

boxee.onBigSkip = function()
{
	boxee.log("user pressed big skip")
	widget = boxee.getActiveWidget();
	//widget.mouseMove(10, 10);
	current_x = getProgressCoordinate(widget);
	current_x = current_x + skip_big;
	if(current_x < x_start) current_x = x_start;
	if(current_x > x_end) current_x = x_end;
	widget.click(current_x, y);
	boxee.notifyPlaybackResumed();
}

// function poll() {
// 	widget = boxee.GetActiveWidget();
// 	current_x = getProgressCoordinate(widget)
// 	boxee.log("current_x:"+current_x);
// }
// setInterval(poll, 1000);